package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.jsonbean.ResGameCompareVersion;
import com.rjwh.dingdong.client.bean.localbean.CompareVersion;
import com.rjwh.dingdong.client.bean.localbean.GameCompareVersion;
import com.rjwh.dingdong.client.bean.localbean.LocalGmaeInfo;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.DownloadUpdateModel;
import com.rjwh.dingdong.client.mvp.view.DownloadUpdateView;
import com.rjwh.dingdong.client.network.HttpWebServiceCallBack;
import com.rjwh.dingdong.client.util.ToastUtil;

import java.util.List;

/**
 * 项目名称：whrjwh-parent
 * 类描述：
 * 创建人：wby
 * 创建时间：2016/7/13 16:51
 * 修改人：wby
 * 修改时间：2016/7/13 16:51
 * 修改备注：
 */
public class DownloadUpdatePresenter extends BasePresenter<DownloadUpdateView> implements HttpWebServiceCallBack {

    private DownloadUpdateModel model;

    public DownloadUpdatePresenter(Context context, DownloadUpdateView mMvpView) {
        super(context, mMvpView);
        model = new DownloadUpdateModel(this);
    }

    public void getServerData() {
        try {
            List<LocalGmaeInfo> hbList = MyApplication.db.findAllByWhere(LocalGmaeInfo.class, "type=1");
            List<LocalGmaeInfo> xyList = MyApplication.db.findAllByWhere(LocalGmaeInfo.class, "type=2");
            JSONArray hbArray = new JSONArray();
            JSONArray xyArray = new JSONArray();
            for (LocalGmaeInfo info : hbList) {
                JSONObject object = new JSONObject();
                object.put("id", info.getPid());
                object.put("version", info.getVersion());
                hbArray.add(object);
            }
            for (LocalGmaeInfo info : xyList) {
                JSONObject object = new JSONObject();
                object.put("id", info.getPid());
                object.put("version", info.getVersion());
                xyArray.add(object);
            }
            JSONObject data = new JSONObject();
            data.put("yx", xyArray);
            data.put("hb", hbArray);
            if (model != null) model.getServerData(data.toJSONString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (getMvpView() != null) {
            getMvpView().setRefreshing(false);
            switch (businessType) {
                case NetConstant.COMPAREVERSION:
                    ResGameCompareVersion resGameCompareVersion = (ResGameCompareVersion) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resGameCompareVersion != null) {
                        GameCompareVersion data = resGameCompareVersion.getData();
                        if (data != null) {

                            List<CompareVersion> hbList = data.getHb();
                            List<CompareVersion> xy = data.getYx();
                            if (hbList.isEmpty() && xy.isEmpty()) {
                                ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_update));
                            }
                            for (CompareVersion vc : hbList) {
                                List<LocalGmaeInfo> tempList = MyApplication.db.findAllByWhere(LocalGmaeInfo.class, "pid=" + vc.getId());
                                if (!tempList.isEmpty()) {
                                    int update = vc.getUpdate();
                                    if (update == 1) {
                                        LocalGmaeInfo tempData = tempList.get(0);
                                        if (tempData.getIsUpdate() == 2) {
                                            continue;
                                        }
                                        tempData.setVersion(vc.getVersion());
                                        tempData.setDownloadUrl(vc.getDownloadurl());
                                        tempData.setIsUpdate(1);
                                        tempData.setType(vc.getResourcetype());
                                        MyApplication.db.update(tempData);
                                    }
                                }
                            }

                            for (CompareVersion vc : xy) {
                                List<LocalGmaeInfo> tempList = MyApplication.db.findAllByWhere(LocalGmaeInfo.class, "pid=" + vc.getId());
                                if (!tempList.isEmpty()) {
                                    int update = vc.getUpdate();
                                    if (update == 1) {
                                        LocalGmaeInfo tempData = tempList.get(0);
                                        if (tempData.getIsUpdate() == 2) {
                                            continue;
                                        }
                                        tempData.setVersion(vc.getVersion());
                                        tempData.setDownloadUrl(vc.getDownloadurl());
                                        tempData.setIsUpdate(1);
                                        tempData.setType(vc.getResourcetype());
                                        MyApplication.db.update(tempData);
                                    }
                                }
                            }
                            if (getMvpView() != null) getMvpView().loadData();
                        }

                    } else {
                        ToastUtil.showToast(getContext(), getContext().getString(R.string.refresh_error));
                    }

                    break;
            }
        }
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }
}
